package com.tdtxdcxm.mynotepad.notedata;

import com.tdtxdcxm.mynotepad.listcontaineritem.NoteListItem;
import com.tdtxdcxm.mynotepad.notedetails.NoteDetailInfoList;
import ohos.aafwk.ability.AbilitySlice;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.*;
import java.util.ArrayList;


class StructFile{
    private File dayfile = null;
    private File[] dayfile_notelistfiles = null;

    public File getDayfile() {
        return dayfile;
    }

    public File[] getDayfile_notelistfiles() {
        return dayfile_notelistfiles;
    }

    public StructFile(File dayfile) {
        this.dayfile = dayfile;
        this.dayfile_notelistfiles = dayfile.listFiles();
    }
}


public class NoteDataManager {
    public static File usernote = new File("/data/user/0/com.tdtxdcxm.mynotepad/usernote");

    public static ArrayList<StructFile> dayfolders = new ArrayList<>();

    public static boolean isgenerate = false;

    public static void generateUserNoteDir(){
        dayfolders.clear();
        File[] usernotelistfiles = usernote.listFiles();
        if(usernotelistfiles != null){
            for(int i = 0;i < usernotelistfiles.length;i++){
                dayfolders.add(new StructFile(usernotelistfiles[i]));
            }
            isgenerate = true;
        }
        else{
            isgenerate = false;
        }
    }

    public static void checkUserNoteDir(){
        System.out.println(usernote.toString()+"开始输出usernote文件夹中全部目录结构：--《《");
        for(int i = 0;i < dayfolders.size();i++){
            System.out.println(dayfolders.get(i).getDayfile().toString()+":--{");
            File[] files = dayfolders.get(i).getDayfile_notelistfiles();
            for(int j = 0;j < files.length;j++){
                System.out.println(files[j].toString());
            }
            System.out.println(dayfolders.get(i).getDayfile().toString()+":--}");
        }
        System.out.println(usernote.toString()+"输出usernote文件夹中全部目录结构完成！--》》");
    }

    public static void isUserNoteExist(){
        if(usernote.exists()){
            System.out.println("usernote is 存在！");
            return;
        }
        System.out.println("usernote is 不存在！");
    }

    public static void readAllNoteMataForList(ArrayList<NoteListItem> listcontainerItems){
        listcontainerItems.clear();//确保是空列表
        for(int i = 0;i < dayfolders.size();i++){
            File[] files = dayfolders.get(i).getDayfile_notelistfiles();
            for(int j = 0;j < files.length;j++){
                File[] detailsfiles = files[j].listFiles();
                if(detailsfiles.length != 0){
                    try{
                        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(detailsfiles[0]) ));
                        StringBuilder stringBuilder = new StringBuilder();
                        String t = null;
                        while ((t = bufferedReader.readLine()) != null) {
                            stringBuilder.append(t);
                        }
                        System.out.println(stringBuilder.toString());
                        listcontainerItems.add(new NoteListItem(stringBuilder.toString(),files[j]));
                        bufferedReader.close();
                    }
                    catch (FileNotFoundException e){
                        e.printStackTrace();
                    }
                    catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
        for(int i = 0;i < listcontainerItems.size();i++){
            System.out.println("检查listcontaineritem列表内容："+listcontainerItems.get(i).getShortText());
        }
    }

    public static ImageSource.SourceOptions sourceOptions(){
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();

        options.formatHint = "image/jpeg";

        return options;
    }

    public static ArrayList<NoteDetailInfoList> readSomeNoteDetails(File file, AbilitySlice abilitySlice){
        System.out.println("readSomeNoteDetails检查file-------->>"+file);
        ArrayList<NoteDetailInfoList> noteDetailInfoLists = new ArrayList<>();

        File[] files = file.listFiles();
        for(int i = 0;i < files.length;i++){
            if(files[i].getName().endsWith("txt")){
                try{
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(files[i])));
                    StringBuilder stringBuilder = new StringBuilder();
                    String t = null;
                    while ((t = bufferedReader.readLine()) != null) {
                        stringBuilder.append(t);
                    }
                    System.out.println(stringBuilder.toString());
                    noteDetailInfoLists.add(new NoteDetailInfoList(stringBuilder.toString(),file,0));

                    bufferedReader.close();
                }
                catch (FileNotFoundException e){
                    e.printStackTrace();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }

            if(files[i].getName().endsWith("jpeg")){
                try{
                    FileInputStream fileInputStream = new FileInputStream(files[i]);
                    ImageSource imageSource = ImageSource.create(fileInputStream.getFD(),sourceOptions());
                    PixelMap pixelMap = imageSource.createPixelmap(null);
                    fileInputStream.close();
                    noteDetailInfoLists.add(new NoteDetailInfoList(pixelMap,file,1));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return noteDetailInfoLists;
    }



    public static File deleteFolderFiles(File folder){
        //删除某文件夹中的所有文件
        File[] filelist = folder.listFiles();
        for(int i = 0;i < filelist.length;i++){
            try
            {
                filelist[i].delete();
            }
            catch (SecurityException e){
                e.printStackTrace();
            }
        }
        return folder;
    }

    private NoteDataManager(){
    }

}
